package com.ruoyi.system.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.YyAppointmentTimeMapper;
import com.ruoyi.system.domain.YyAppointmentTime;
import com.ruoyi.system.service.IYyAppointmentTimeService;
import com.ruoyi.common.core.text.Convert;

/**
 * 预约时间段Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-25
 */
@Service
public class YyAppointmentTimeServiceImpl implements IYyAppointmentTimeService
{
    @Autowired
    private YyAppointmentTimeMapper yyAppointmentTimeMapper;

    /**
     * 查询预约时间段
     *
     * @param id 预约时间段主键
     * @return 预约时间段
     */
    @Override
    public YyAppointmentTime selectYyAppointmentTimeById(Long id)
    {
        return yyAppointmentTimeMapper.selectYyAppointmentTimeById(id);
    }

    /**
     * 查询预约时间段列表
     *
     * @param yyAppointmentTime 预约时间段
     * @return 预约时间段
     */
    @Override
    public List<YyAppointmentTime> selectYyAppointmentTimeList(YyAppointmentTime yyAppointmentTime)
    {
        return yyAppointmentTimeMapper.selectYyAppointmentTimeList(yyAppointmentTime);
    }

    /**
     * 新增预约时间段
     *
     * @param yyAppointmentTime 预约时间段
     * @return 结果
     */
    @Override
    public int insertYyAppointmentTime(YyAppointmentTime yyAppointmentTime)
    {
        return yyAppointmentTimeMapper.insertYyAppointmentTime(yyAppointmentTime);
    }

    /**
     * 修改预约时间段
     *
     * @param yyAppointmentTime 预约时间段
     * @return 结果
     */
    @Override
    public int updateYyAppointmentTime(YyAppointmentTime yyAppointmentTime)
    {
        return yyAppointmentTimeMapper.updateYyAppointmentTime(yyAppointmentTime);
    }

    /**
     * 批量删除预约时间段
     *
     * @param ids 需要删除的预约时间段主键
     * @return 结果
     */
    @Override
    public int deleteYyAppointmentTimeByIds(String ids)
    {
        return yyAppointmentTimeMapper.deleteYyAppointmentTimeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除预约时间段信息
     *
     * @param id 预约时间段主键
     * @return 结果
     */
    @Override
    public int deleteYyAppointmentTimeById(Long id)
    {
        return yyAppointmentTimeMapper.deleteYyAppointmentTimeById(id);
    }

    /**
     * 查询今天哪个时间端还能预约
     */
    @Override
    public List<Map<String, Object>> selectAppointmentTime(Integer doctorId, Integer departmentId) {
        Date date = new Date();
        if (doctorId == null && departmentId == null){
            throw new RuntimeException("参数错误");
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String str = dateFormat.format(date);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        List<YyAppointmentTime> yyAppointmentTimes = yyAppointmentTimeMapper.selectYyAppointmentTimeList(new YyAppointmentTime());
        List<YyAppointmentTime> yyAppointmentTimes = yyAppointmentTimeMapper.selectYyAppointment(doctorId,departmentId);
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < yyAppointmentTimes.size(); i++) {
            YyAppointmentTime yyAppointmentTime = yyAppointmentTimes.get(i);
            String[] split = yyAppointmentTime.getAppointmentTime().split("-");
            String d1 = split[0];
            String d2 = split[1];
            String s3 = str + " " + d2 + ":00";
            try {
                Date parse = format.parse(s3);
                if (parse.getTime() > date.getTime()){
                    if(StrUtil.isEmpty(yyAppointmentTime.getStatus()) && yyAppointmentTime.getDepartmentId() == departmentId){
                        Map<String, Object> stringObjectHashMap = new HashMap<>();
                        stringObjectHashMap.put("time",yyAppointmentTime.getAppointmentTime());
                        list.add(stringObjectHashMap);
                    }
                }
            } catch (ParseException e) {
                throw new RuntimeException("系统错误");
            }
        }
        return list;
    }

    @Override
    public void updateBatch(List<YyAppointmentTime> yyAppointmentTimes) {
        yyAppointmentTimeMapper.updateBatch(yyAppointmentTimes);
    }
}
